using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class HelpCategoryEditor : UserControl, ISavableControl
    {
        private bool _hasChanges = false;

        private HelpCategory _helpCategory = null;

        public HelpCategory CurrentHelpCategory
        {
            get { return _helpCategory; }
            set { _helpCategory = value; }
        }

        public HelpCategoryEditor()
        {
            InitializeComponent();
            this.Leave += new EventHandler(CheckUnsavedData);
        }

        public void LoadHelpCategory()
        {
            if (CurrentHelpCategory != null)
            {
                textBoxName.Text = CurrentHelpCategory.name;
                textBoxId.Text = CurrentHelpCategory.id;
                textBoxCreatedDate.Text = CurrentHelpCategory.createdDate;
                textBoxChangedDate.Text = CurrentHelpCategory.changedDate;
                textBoxAuthor.Text = CurrentHelpCategory.author;
                LoadVersions();
            }
        }

        private void LoadVersions()
        {
            if (string.IsNullOrEmpty(CurrentHelpCategory.version))
                CurrentHelpCategory.version = HCProjectManager.CurrentProject.ProjectManifest.version;

            string verionsString = CurrentHelpCategory.version;
            
            string[] strs = System.Text.RegularExpressions.Regex.Split(verionsString, "\\.");
            if (strs.Length != 4)
                throw new Exception(string.Concat("Version number must have four parts, error version string: ", verionsString));
            textBoxVersion1.Text = strs[0];
            textBoxVersion2.Text = strs[1];
            textBoxVersion3.Text = strs[2];
            textBoxVersion4.Text = strs[3];
        }

        public bool HasUnsavedData
        {
            get
            {
                bool result = false;

                if (_hasChanges) return _hasChanges;

                if (string.Compare(textBoxName.Text, CurrentHelpCategory.name, false) != 0)
                    result = true;

                if (string.Compare(textBoxId.Text, CurrentHelpCategory.id, false) != 0)
                    result = true;

                if (string.Compare(textBoxAuthor.Text, CurrentHelpCategory.author, false) != 0)
                    result = true;

                if (string.Compare(ReadVersionString(), CurrentHelpCategory.version, false) != 0)
                    result = true;

                return result;
            }
        }

        private string ReadVersionString()
        {
            return string.Concat(textBoxVersion1.Text, ".", textBoxVersion2.Text, ".", textBoxVersion3.Text, ".", textBoxVersion4.Text);
        }

        public void SaveData()
        {
            if (HasUnsavedData)
            {
                if (string.IsNullOrEmpty(textBoxId.Text))
                {
                    MessageBox.Show("Help Item must have Id, cannot save", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                CurrentHelpCategory.name = textBoxName.Text;
                CurrentHelpCategory.id = textBoxId.Text;
                CurrentHelpCategory.changedDate = DateTime.Now.ToString(Utils.DEFAULT_DATETIME_FORMAT_STRNG);
                CurrentHelpCategory.author = textBoxAuthor.Text;
                CurrentHelpCategory.version = ReadVersionString();

                //Manifest entry
                CurrentHelpCategory.ManifestEntry.id = CurrentHelpCategory.id;

                HelpCategory tmp = HCProjectManager.CurrentProject.HelpCategory.Find(delegate(HelpCategory cat) { return string.Compare(cat.id, CurrentHelpCategory.id, true) == 0; });
                if (tmp == null)
                    HCProjectManager.CurrentProject.HelpCategory.Add(CurrentHelpCategory);
                else
                    tmp = CurrentHelpCategory;
            }
        }

        public void CheckUnsavedData(object sender, EventArgs e)
        {
            if (this.HasUnsavedData)
            {
                this.SaveData();
                Global.HAS_CHANGES_IN_MEMORY_NOT_SAVED_TO_DISK = true;
            }
        }

        private void btnParentHelpItem_Click(object sender, EventArgs e)
        {
            using (ParentHelpItemChooser chooser = new ParentHelpItemChooser())
            {
                chooser.LoadData(CurrentHelpCategory.ManifestEntry);
                DialogResult dResult = chooser.ShowDialog(this);
                if (dResult == DialogResult.OK)
                {
                    _hasChanges = chooser.HasChanges;
                    CurrentHelpCategory.ManifestEntry.Parents = chooser.ParentEntries;
                }
            }
        }

        private void buttonRelatedItem_Click(object sender, EventArgs e)
        {
            using (RelatedItemChooser chooser = new RelatedItemChooser())
            {
                chooser.LoadData(CurrentHelpCategory.ManifestEntry);
                DialogResult dResult = chooser.ShowDialog(this);
                if (dResult == DialogResult.OK)
                {
                    _hasChanges = chooser.HasChanges;
                    CurrentHelpCategory.ManifestEntry.relatedItemsPoitingToMe = chooser.RelatedItems;
                }
            }
        }

        private void DigitOnly(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Digit only", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                e.Handled = true;
            }
        }
    }
}
